using System;
using System.Text;
using System.Web.UI;
using mojoPortal.Web;

namespace ud_Menu
{
    public partial class MenuModule : SiteModuleControl
    {
        private MenuConfiguration config = new MenuConfiguration();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Load += Page_Load;
            EnableViewState = false;
        }

        protected virtual void Page_Load(object sender, EventArgs e)
        {
            SetupJavaScript();
            LoadSettings();
            PopulateLabels();
            PopulateControls();
        }

        private void SetupJavaScript()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script type='text/javascript'>");
            sb.Append("$(document).ready(function(){");
            sb.Append("$('.module" + ModuleId + " ul').lavaLamp({speed: 300});");
            sb.Append("});");
            sb.Append("</script>");
            if (!Page.ClientScript.IsStartupScriptRegistered(typeof(Page), "lampscript"))
            {
                Page.ClientScript.RegisterStartupScript(typeof(Page), "lampscript", "<script type='text/javascript' src='/ClientScript/jqmojo/jquery.lavalamp-1.3.5.min.js'></script>");
            }
            if (!Page.ClientScript.IsStartupScriptRegistered(typeof(Page), "lamp" + ModuleId))
            {
                Page.ClientScript.RegisterStartupScript(typeof (Page), "lamp" + ModuleId, sb.ToString());
            }
        }

        private void PopulateControls()
        {
            if (!config.UseArtisteer) return;
            ltrMenuBegin.Text = @"<div class='art-nav'><div class='l'></div><div class='r'></div>";
            ltrMenuEnd.Text = @"</div>";
        }

        protected virtual void PopulateLabels()
        {

        }

        protected virtual void LoadSettings()
        {
            pnlContainer.ModuleId = ModuleId;
            config = new MenuConfiguration(Settings);
            if (!config.CustomCssClass.Equals(string.Empty)) { pnlContainer.CssClass += " " + config.CustomCssClass; }
            siteMenu1.Direction = config.Direction;
            siteMenu1.DynamicDisplayLevels = config.DynamicDisplayLevels;
            siteMenu1.HideMenuOnSiteMap = config.HideMenuOnSiteMap;
            siteMenu1.IncludeCornerRounders = config.IncludeCornerRounders;
            siteMenu1.StartingNodeOffset = config.StartingNodeOffset;
            siteMenu1.SuppressPageSelection = config.SuppressPageSelection;
            siteMenu1.TopLevelOnly = config.TopLevelOnly;
            siteMenu1.TreeViewExpandDepth = config.TreeViewExpandDepth;
            siteMenu1.TreeviewPopulateNodesFromClient = config.TreeviewPopulateNodesFromClient;
            siteMenu1.TreeViewPopulateOnDemand = config.TreeViewPopulateOnDemand;
            siteMenu1.TreeViewShowExpandCollapse = config.TreeViewShowExpandCollapse;
            siteMenu1.Use3SpansInLinks = config.Use3SpansInLinks;
            siteMenu1.UseArtisteer = config.UseArtisteer;
            siteMenu1.UseSpanInLinks = config.UseSpanInLinks;
            siteMenu1.UseSuperfish = config.UseSuperfish;
            siteMenu1.UseTreeView = config.UseTreeView;
            lblHeader.Text = config.HeaderText;
            lblFooter.Text = config.FooterText;
        }

    }
}
